package com.taoyuanx.boot.utils;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @author admin
 * hash 工具类
 */
public class HashUtil {
	public static final String MD5="0";
	public static final String SHA256="1";
	public static final Map<String,MessageDigest> hashMap=new HashMap<>();
	private static final String PWD_SALT=PropertiesUtil.getSystemProperty("pwd_salt");//密码 盐
	static{
		hashMap.put(MD5,DigestUtils.getMd5Digest());
		hashMap.put(SHA256,DigestUtils.getSha256Digest());
	}
	public static String hash(String hashId,byte[] content){
		switch (hashId) {
		case MD5:
			return Base64.encodeBase64String(hashMap.get(MD5).digest(content));
		case SHA256:
			return Base64.encodeBase64String(hashMap.get(SHA256).digest(content));
		default :
			return Base64.encodeBase64String(hashMap.get(MD5).digest(content));
		
		}
	}
	public static String pwdHash(String pwd){
		return Hex.encodeHexString(hashMap.get(MD5).digest((pwd+PWD_SALT).getBytes()));
	}
	public static void main(String[] args) {
		System.out.println(Hex.encodeHexString(hashMap.get(MD5).digest("1ef9f3c4b".getBytes())));
		System.out.println(pwdHash("1"));
	}
}
